package pagefetch.control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFileChooser;

import pagefetch.model.HTMLPage;
import pagefetch.view.FetcherPanel;
import pagefetch.view.OutputFrame;

public class FetcherController implements ActionListener {

	FetcherPanel view;
	JFileChooser chooser;
	private String choosertitle = "Verkennen";

	public FetcherController(FetcherPanel fetcherPanel) {
		view = fetcherPanel;
		view.getFetchButton().addActionListener(this);
		view.getFilechooserButton().addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(view.getFetchButton())) {
			String site = view.getUrlField().getText();
			HTMLPage page = HTMLPage.getPage(site, 1);
			new OutputFrame(page.getContent());
		} else if (e.getSource().equals(view.getFilechooserButton())) {
			chooser = new JFileChooser();
			chooser.setCurrentDirectory(new java.io.File("."));
			chooser.setDialogTitle(choosertitle);
			chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			chooser.setAcceptAllFileFilterUsed(false);
			if (chooser.showOpenDialog(view) == JFileChooser.APPROVE_OPTION) {
				view.setFileChooserField(chooser.getSelectedFile().toString());
			} else {
				view.setFileChooserField("No Selection");
			}
		}
	}

}
